package cn.xt.demo.servlet;

import cn.xt.demo.config.NetConfig;
import cn.xt.demo.utils.Md5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Calendar;

/**
 * Created by heyue on 2017/6/5.
 */
@WebServlet(name = "VoiceSmsServlet")
public class VoiceSmsServlet extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("UTF-8");
//            String appAccountId = request.getParameter("appAccountId");
            long FY_TIMEINMILLIS = Calendar.getInstance().getTimeInMillis();//实际开发 不能用 static 不然值是固定的
            String FY_AU = Md5Util.md532upper(NetConfig.FY_APPID + NetConfig.FY_APPTOKEN + FY_TIMEINMILLIS);//MD5（FY_APPID+ FY_APPTOKEN+ti）

//            String receiverPhone = "8618124089514";
            String receiverPhone = "8618584500435";
            String mynet2FyRequestString="?appId="+NetConfig.FY_APPID
                    +"&callee="+ receiverPhone
                    +"&playtimes="+ 3
                    +"&attemptInterval="+ 1
                    +"&minDigits="+ 0
                    +"&maxDigits="+ 0
                    +"&msg="+ "12345678我是短信内容"
//                    +"&msg="+ "111111112323232323232323"
                    +"&ti="+FY_TIMEINMILLIS
                    +"&au="+FY_AU;


            response.setContentType("text/html;charset=UTF-8");
            PrintWriter writer = response.getWriter();
//            mynet2FyRequestString = URLEncoder.encode(mynet2FyRequestString, "utf-8");

            String urlString = "http://sms.feiyucloud.com/v3/playSoundMsg"+mynet2FyRequestString;
            String res = "";

            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestProperty("contentType", "UTF-8");
            conn.setRequestProperty("Content-type", "text/html");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestMethod("POST");
            InputStream inputStream = conn.getInputStream();
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream,"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line+"\n";
            }
            in.close();
            writer.write(res);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("error in wapaction,and e is " + e.getMessage());
        }



    }

}
